<?php
  defined('_JEXEC') or die();
  jimport('joomla.application.component.model');
  require_once('base.php');
  
  class ScheduleModelSchedule extends BaseModel
  {     
    var $_name        = "schedule";
    var $_table       = "#__pbny_schedules";
    
    function get_by_team_id($team){
      $sql = 'SELECT * '.
             'FROM '.$this->_table.
           " WHERE first_team_id=$team OR second_team_id=$team";
      return $this->_get($sql);
    }
    
    function get_summary_list($id = null){
    
      $t_conference   = '#__pbny_conferences';
      $t_venue        = '#__pbny_venues';
      $t_team         = '#__pbny_teams';
      
      $sql = 'SELECT s.*, c.name as conference,
            v.name as venue,
            t1.name as team1,
            t2.name as team2 ' .
             'FROM ' . $this->_table . ' s ' .
           "LEFT JOIN $t_conference c ON c.id=s.conference_id ".
           "LEFT JOIN $t_venue v ON v.id=s.venue_id ".
           "LEFT JOIN $t_team t1 ON t1.id=s.first_team_id ".
           "LEFT JOIN $t_team t2 ON t2.id=s.second_team_id ".
      
      $where = null;
      if($id)
        $where = ' WHERE id = ' . $id;
      
      $this->_sql = $sql;
          
      return $this->getListData();
    }
    
    function get_by_schedule($start_date, $end_date, $team_id = null, $conference_id = null, $venue_id = null){

      $where = " WHERE schedule BETWEEN '$start_date' AND '$end_date'";
            $order_by = ' ORDER BY schedule ASC';
                 
      if($team_id)
        $where .= " AND (first_team_id = $team_id OR second_team_id = $team_id)";

      if($conference_id)
        $where .= " AND conference_id = $conference_id";
      
      if($venue_id)
        $where .= " AND venue_id = $venue_id";
    
      $sql = 'SELECT * '.
             'FROM '.$this->_table.
           $where.
           $order_by;
             

      return $this->_get($sql);
      
    }
  }
?>
